<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\User;
use App\Models\WorkAttendanceRecord;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class WorkAttendanceRecordController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];

        if ( $request->id) {
            $work_attendance_record = WorkAttendanceRecord::findOrFail($request->id);
            $work_attendance_record->mtime = time();
        } else {
            $work_attendance_record = new WorkAttendanceRecord();
            $work_attendance_record->ctime = time();
        }
        $work_attendance_record->employee_id = $request->employee_id;
        $work_attendance_record->company_id = $company_id;
        $work_attendance_record->shift_id = $request->shift_id;
        $work_attendance_record->clock_in_date = $request->clock_in_date;
        $work_attendance_record->clock_in_hours = $request->clock_in_hours;
        $work_attendance_record->source = $request->source;
        $work_attendance_record->devices = $request->devices;
        $work_attendance_record->remark = $request->remark;
        $work_attendance_record->clock_in_place = $request->clock_in_place;
        $work_attendance_record->first_stage_work = $request->first_stage_work;
        $work_attendance_record->first_stage_duty = $request->first_stage_duty;
        $work_attendance_record->second_stage_work = $request->second_stage_work;
        $work_attendance_record->second_stage_duty = $request->second_stage_duty;
        $work_attendance_record->third_stage_work = $request->third_stage_work;
        $work_attendance_record->third_stage_duty = $request->third_stage_duty;
        $work_attendance_record->uid = $uid;

        $work_attendance_record->save();

        return showError(Error::ERR_OK,['id'=>$work_attendance_record->id]);

    }

    public function show(Request $request) {
        $work_attendance_record = WorkAttendanceRecord::findOrFail($request->id);
        return showError(Error::ERR_OK,['work_attendance_record'=>$work_attendance_record]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = WorkAttendanceRecord::getList($where);
        foreach ($list as $k => &$v) {

            $v->employee_info = Employee::where('employee_id',$v->employee_id)->where('company_id',$company_id)->where('status',1)->first();
        }
        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        WorkAttendanceRecord::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
